﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageWorkingTaskList : BaseForm.frmBaseMdiChildForm
    {
        private ProdPlanOrderItemEntity mSelectedPlanOrderItem;

        public frmManageWorkingTaskList()
        {
            InitializeComponent();
        }

        private void BindCombobo()
        {
            DataTable dt = ProdTechnicBs.GetProdTechnicAll().Tables[0];
            DataRow dr = dt.NewRow();
            dt.Rows.InsertAt(dr,0);
            cmbTechnic.DataSource = dt;
            cmbTechnic.ValueMember = "TECHNIC_ID";
            cmbTechnic.DisplayMember = "TECHNIC_NAME";

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "PDTYPE", false);
        }

        private string GetCondition()
        {
            string Condition = "";
            if (txtWorkingGroup.Tag != null)
            {
                Condition += " AND GROUP_ID='" + ((MstWorkingGroupEntity)txtWorkingGroup.Tag).GroupId + "' ";
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                Condition += " AND DELIVERY_DATE>='" + ((DateTime)dtpDeliveryDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00' ";
            }
            if (dtpDeliveryDateTo.EditValue!=null)
            {
                Condition += " AND DELIVERY_DATE<='" + ((DateTime)dtpDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            if (cmbTechnic.SelectedValue != null && string.IsNullOrEmpty(cmbTechnic.SelectedValue.ToString()) == false)
            {
                Condition += " AND TECHNIC_ID='" + cmbTechnic.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(txtPlanbOrderItemCode.Text)==false)
            {
                Condition += " AND ORDER_ITEM_CODE LIKE '%" + txtPlanbOrderItemCode.Text+"%' ";
            }
            if (string.IsNullOrEmpty(txtProdPlanOrderCode.Text) == false)
            {
                Condition += " AND PLAN_ORDER_CODE LIKE '%"+txtProdPlanOrderCode.Text+"%' ";
            }
            if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            {
                Condition += " AND CONTRACT_MODEL LIKE '%" + txtContractModel.Text + "%' ";
            }
            return Condition;
        }

        private void BindGrid()
        {
            string Condition = GetCondition();
            DataTable dt = ProdProductTechnicBs.GetDtWorkingGroupList(Condition,txtRowCount.Text);
            gvMain.DataSource = dt;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobo();
            BindGrid();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle < 0)
                return;
            DataRow dr = gridView3.GetDataRow(e.FocusedRowHandle);
            if (dr == null)
                return;
            mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(dr["PLAN_ORDER_ITEM_ID"].ToString());
            if (mSelectedPlanOrderItem != null)
            {
                DataTable dt = ProdPlanOrderItemBs.GetAssembleTaskList(mSelectedPlanOrderItem.PlanOrderItemId);
                gvDetail.DataSource = dt;
            }
        }

        private void txtWorkingGroup_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainWorkingGroupQuery frm = new frmMaintainWorkingGroupQuery();
                if (frm.ShowDialog() == DialogResult.OK && frm.SelectedMstWorkingGroupPerson != null)
                {
                    MstWorkingGroupEntity m = MstWorkingGroupBs.GetMstWorkingGroupByID(frm.SelectedMstWorkingGroupPerson.GroupId);
                    txtWorkingGroup.Text = m.GroupName;
                    txtWorkingGroup.Tag = m;
                }
                else
                {
                    txtWorkingGroup.Text = "";
                    txtWorkingGroup.Tag = null;
                }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (e.Column.FieldName.Equals("PRODUCT_CODE"))
            //{
            //    string sProductCode = e.CellValue.ToString();
            //    try
            //    {
            //        EsolWevServices.EsolService ws = new EsolWevServices.EsolService();
            //        byte[] arrFileData = ws.DownloadFile(sProductCode + ".jpg");
            //        Image img = CommonFunctions.byteArrayToImage(arrFileData);
            //        frmProdBlueprintViewer frm = new frmProdBlueprintViewer(img);
            //        frm.ShowDialog();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("图片不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //}
        }

        private void gridView2_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (e.Column.FieldName.Equals("PRODUCT_CODE"))
            //{
            //    string sProductCode = e.CellValue.ToString();
            //    try
            //    {
            //        EsolWevServices.EsolService ws = new EsolWevServices.EsolService();
            //        byte[] arrFileData = ws.DownloadFile(sProductCode + ".jpg");
            //        Image img = CommonFunctions.byteArrayToImage(arrFileData);
            //        frmProdBlueprintViewer frm = new frmProdBlueprintViewer(img);
            //        frm.ShowDialog();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("图片不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //}
        }

    }
}
